﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using pTools.cMethods;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

namespace pTools.TuQiangPrint
{
    public partial class SelectSBInfo : DevExpress.XtraEditors.XtraForm
    {
        DBHelper db = new DBHelper();

        //List<object> list;

        public DataTable selected ;

        public string type;

        string iCode,iName;//检索条件


        public SelectSBInfo()
        {
            InitializeComponent();

            //selectedList = new List<DbShuiXiBiaoInfo>();
        }

        private void NewSelectInventory_Load(object sender, EventArgs e)
        {
            LoadInfos();
        }

        void LoadInfos()
        {
            if (type == "ca")
            {
                LoadCarInfo();
            }
            else if (type == "ma")
            {
                LoadMachineInfo();
            }
        }

        void LoadCarInfo()
        {
            var sql = @"select cCode scode ,cName sname from fun_Cars_Info where isdel=0 ";
            if (!string.IsNullOrEmpty(iCode))
            {
                sql += " and cCode like '%" + iCode + "%' ";
            }
            if (!string.IsNullOrEmpty(iName))
            {
                sql += " and cName like '%" + iName + "%' ";
            }
            
            var dt = db.getData(sql);

            //list = CommonMethods.ConvertTo<DbSheBeiBar>(dt);
            //grid_Invtory.DataSource = list;
            grid_Invtory.DataSource = dt;
            selected = dt.Clone();
        }
        
        void LoadMachineInfo()
        {
            var sql = @"select mCode scode ,mName sname from fun_machine_info where isdel=0  ";  //and mType ='zbj'
            if (!string.IsNullOrEmpty(iCode))
            {
                sql += " and mCode like '%" + iCode + "%' ";
            }
            if (!string.IsNullOrEmpty(iName))
            {
                sql += " and mName like '%" + iName + "%' ";
            }
            
            var dt = db.getData(sql);

            //list = CommonMethods.ConvertTo<DbSheBeiBar>(dt);
            //grid_Invtory.DataSource = list;
            grid_Invtory.DataSource = dt;
            selected = dt.Clone();
        }

        private void tb_EditValueChanged(object sender, EventArgs e)
        {
            iCode = tb_code.Text;
            iName = tb_name.Text;

            LoadInfos();
        }


        private void btn_Confirm_Click(object sender, EventArgs e)
        {
            var rows = gridView_Invtory.GetSelectedRows();
            foreach (var i in rows)
            {
                var a = gridView_Invtory.GetDataRow(i);
                selected.ImportRow(gridView_Invtory.GetDataRow(i));
            }
            //selectedList = list.Where(
            //    r =>
            //        gridView_Invtory.GetSelectedRows()
            //        .Contains(list.IndexOf(r)))
            //        .ToList();

            this.Close();
        }
    }
}
